﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Globalization;
using System.Windows.Data;

#endregion

namespace ScrumTable.UI.View.Converter
{


    /// <summary>
    /// Convert string -> double?
    /// </summary>
    [ValueConversion(typeof(string), typeof(double?))]
    public class ConverterNullableDouble : IValueConverter
    {
        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Converts value (preferably a double?) into a String.
        /// if value==null => ""
        /// else value.ToString()
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// if value==null => ""
        /// else value.ToString()
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if( value == null)
            {
                return "";
            }
            return value.ToString();
        }

        /// <summary>
        /// Converts the string to a double
        /// if "" -> double? == null
        /// else -> value.toString()
        /// </summary>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// Resulting double or double? which is null if that was not possible.
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double? ret = null;             
            if(value.ToString() == String.Empty)
            {
                return ret;
            }
            return value;
        }

      
        #endregion
       
    }
}
